package com.frx.leetoffer.binarytree;


import java.util.LinkedList;
import java.util.Queue;

/**
 * @author ：frx
 * @date ：Created in 2019/11/5 21:41
 * @description：ToDO
 * @modified By：
 * @version: ToDO
 */
public class OrderTree {

    private static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        public TreeNode(int val){
            this.val = val;
        }
        public TreeNode(int val,TreeNode left,TreeNode right){
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static void main(String[] args){
        TreeNode treeNode = new TreeNode(2);
        TreeNode treeNode2 = new TreeNode(3);
        TreeNode treeNode3 = new TreeNode(1,treeNode,treeNode2);
        TreeNode treeNode4 = new TreeNode(4);
        TreeNode treeNode5 = new TreeNode(5);
        TreeNode treeNode6 = new TreeNode(6,treeNode4,treeNode5);
        TreeNode treeNode7 = new TreeNode(7,treeNode3,treeNode6);

        levelOrder(treeNode7);
    }

    public static void levelOrder(TreeNode root){

        Queue<TreeNode> queue = new LinkedList<>();
        ((LinkedList<TreeNode>) queue).add(root);
        while (!queue.isEmpty()){
            TreeNode cur = ((LinkedList<TreeNode>) queue).remove();
            System.out.print(cur.val + " ");
            if (cur.left != null){
                ((LinkedList<TreeNode>) queue).add(cur.left);
            }
            if (cur.right != null){
                ((LinkedList<TreeNode>) queue).add(cur.right);
            }
        }


    }

}

